/*************************************************************************
	> File Name: P3029.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-01-03 13:13:32
 ************************************************************************/
#include <iostream>
#include <map>
#include <deque>
#include <algorithm>
using namespace std;

#define MAX_N 50000

struct Node {
    int x, id;
} cow[MAX_N + 5];

map<int, int> mp;

bool cmp(Node &p, Node &q) {
    return p.x < q.x;
}

int main() {
    int n, sum = 0, ans = INT32_MAX;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> cow[i].x >> cow[i].id;
        mp[cow[i].id]++;
        if (mp[cow[i].id] == 1) sum++;
    }
    sort(cow + 1, cow + n + 1, cmp);
    mp.clear();
    int cnt = 0;
    deque<int> q;
    for (int i = 1; i <= n; i++) {
        if (mp[cow[i].id] == 0) cnt++;
        mp[cow[i].id]++;
        q.push_back(i);
        while (!q.empty() && mp[cow[q.front()].id] > 1) {
            mp[cow[q.front()].id]--;
            q.pop_front();
        }
        if (cnt == sum) {
            ans = min(ans, cow[q.back()].x - cow[q.front()].x);
        }
    }
    cout << ans << endl;
    return 0;
}
